<?php

/**
 * 特色商品管理
 *
 * @author libaoling
 * @time 2014-12-1
 */
class FeatureApp extends BackendApp
{

    private $_feature_mod;

    public function __construct()
    {
        $this->FeatureApp();
    }

    public function FeatureApp()
    {
        parent::__construct();

        $this->_feature_mod = & m('features');
    }

    public function index()
    {
        $conditions = $this->_get_query_conditions(array(
            array(
                'field' => 'title',
                'equal' => 'like',
            ),
            array(
                'field' => 'g.goods_name',
                'name'  => 'goods_name',
                'equal' => 'like',
            ),
            array(
                'field' => 'fg.goods_id',
                'name'  => 'goods_id',
                'type' => 'int',
            ),
        ));

        if (isset($_GET['sort']) && isset($_GET['order']))
        {
            $sort = strtolower(trim($_GET['sort']));
            $order = strtolower(trim($_GET['order']));
            if (!in_array($order, array('asc', 'desc')))
            {
                $sort = 'this.add_time';
                $order = 'desc';
            }
        }
        else
        {
            $sort = 'this.add_time';
            $order = 'desc';
        }

        $page = $this->_get_page();
        $feature_list = $this->_feature_mod->find(array(
            'conditions' => "g.is_feature = 1" . $conditions,
            'join' => 'has_goods',
            'fields' => 'this.*, g.goods_name, g.price',
            'count' => true,
            'order' => "$sort $order",
            'limit' => $page['limit'],
        ));

        $this->assign("feature_list", $feature_list);
        $this->assign('enable_radar', Conf::get('enable_radar'));
        $page['item_count'] = $this->_feature_mod->getCount();
        $this->import_resource(array('script' => 'inline_edit.js'));
        $this->_format_page($page);
        $this->assign('page_info', $page);

        $this->display("feature.index.html");
    }

    public function ajax_col()
    {
        $id = empty($_GET['id']) ? 0 : intval($_GET['id']);
        $column = empty($_GET['column']) ? '' : trim($_GET['column']);
        $value = isset($_GET['value']) ? trim($_GET['value']) : '';
        $data = array();

        if (in_array($column, array('title', 'sort')))
        {
            $data[$column] = $value;
            $this->_feature_mod->edit($id, $data);
            if (!$this->_feature_mod->has_error())
            {
                $this->admin_log('编辑特色商品:' . $id);
                echo ecm_json_encode(true);
            }
        }
        else
        {
            return;
        }
        return;
    }

    public function drop()
    {
        $id = isset($_GET['id']) ? trim($_GET['id']) : '';
        if (!$id)
        {
            $this->show_warning('没有此特色商品');
            return;
        }

        $ids = explode(',', $id);
        
        $this->admin_log('删除特色商品:' . $id);

        if (!$this->_feature_mod->drop($ids))
        {
            $this->show_warning($this->_feature_mod->get_error());
            return;
        }else{
            $goods_mod = m("goods");
            $goods_mod->edit($ids, array('is_feature' => 0));
        }

        $ret_page = isset($_GET['ret_page']) ? intval($_GET['ret_page']) : 1;
        $this->show_message('删除成功', 'back_list', 'index.php?app=feature&page=' . $ret_page);
    }

}
